package org.jeecg.modules.rooms.controller;


import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.Enum.ResultCode;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.rooms.DTO.CesGoodsUnitDeleteBatchDTO;
import org.jeecg.modules.rooms.DTO.CesGoodsUnitDto;
import org.jeecg.modules.rooms.DTO.CesGoodsUnitSearchDto;
import org.jeecg.modules.rooms.service.CesGoodsUnitServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品单位 前端控制器
 * </p>
 *
 * @author Claude
 * @since 2023-03-08
 */
@RestController
@RequestMapping("/rooms/cesGoodsUnit")
public class CesGoodsUnitController {

    @Resource
    private CesGoodsUnitServiceImpl cesGoodsUnitService;


    /***
     * 查询单位
     * @param dto
     * @return
     */
    @ApiOperation(value="查询单位", notes="查询单位")
    @GetMapping(value = "/list")
    public Result list(CesGoodsUnitSearchDto dto){
        if(StringUtils.isBlank(dto.getHotelId())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(dto.getPageNo().intValue() <= 0){
            return Result.error("页数不能小于等于0");
        }
        if(dto.getPageSize().intValue() < 10){
            return Result.error("分页条数不能小于10");
        }
        try{
            return cesGoodsUnitService.list(dto);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @AutoLog(value = "创建单位")
    @ApiOperation(value="创建单位", notes="创建单位")
    @PostMapping(value = "/save")
    public Result create(@RequestBody CesGoodsUnitDto dto){
        if(StringUtils.isBlank(dto.getHotelId())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(dto.getName())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(dto.getState() == null){
            return Result.error(ResultCode.PARAM_MISS);
        }
        try{
            return  cesGoodsUnitService.create(dto);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @AutoLog(value = "修改单位")
    @ApiOperation(value="修改单位", notes="修改单位")
    @PutMapping(value = "/modify")
    public Result modifyCesGoodsUnit(@RequestBody CesGoodsUnitDto dto){
        if(StringUtils.isBlank(dto.getId())){
            return Result.error(ResultCode.PARAM_MISS,"数据ID 必传！");
        }
        if(StringUtils.isBlank(dto.getHotelId())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(dto.getName())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(dto.getState() == null){
            return Result.error(ResultCode.PARAM_MISS);
        }
        try{
            return  cesGoodsUnitService.modify(dto);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }


    @AutoLog(value = "删除单位")
    @ApiOperation(value="删除单位", notes="删除单位")
    @DeleteMapping(value = "/remove")
    public Result remove(String id){
        if(StringUtils.isBlank(id)){
            return Result.error(ResultCode.PARAM_MISS,"数据ID 必传！");
        }
        try{
            return  cesGoodsUnitService.delete(id);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @AutoLog(value = "批量删除单位")
    @ApiOperation(value="批量删除单位", notes="批量删除单位")
    @PostMapping(value = "/deleteBatch")
    public Result deleteBatch(@RequestBody CesGoodsUnitDeleteBatchDTO dto){
        if(StringUtils.isBlank(dto.getIdsStr())){
            return Result.error(ResultCode.PARAM_MISS,"数据IDStr 必传！");
        }
        try{
            List<String> ids = Arrays.stream(dto.getIdsStr().split(",")).collect(Collectors.toList());
            return  cesGoodsUnitService.deleteBatch(ids);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }


}

